using UnityEngine;

[CreateAssetMenu]
public class EnemyFactory : GameObjectFactory
{
    [System.Serializable]
    class EnemyConfig
    {//配置

        public Enemy prefab = default;

        [ FloatRangeSlider(0.5f, 2f)]
        public FloatRange scale = new FloatRange(1f);//设置敌人缩放比例

        [ FloatRangeSlider(0.2f, 5f)]
        public FloatRange speed = new FloatRange(1f);//随机速度

        [ FloatRangeSlider(-0.4f, 0.4f)]
        public FloatRange pathOffSet = new FloatRange(0f);//路径偏移

        [FloatRangeSlider(10f, 10000f)]
        public FloatRange health = new FloatRange(100f);//敌人生命
    }

    [SerializeField]
    EnemyConfig small = default, medium = default, large = default;
    
    //获取配置
    EnemyConfig GetConfig(EnemyType type )
    {
        switch(type)
        {
            case EnemyType.Small: return small;
            case EnemyType.Medium: return medium;
            case EnemyType.Large: return large;
        }
        Debug.Assert(false, "Unsupported enemy type!");
        return null;
    }

    //实例化并获取一个怪物
    public Enemy Get(EnemyType type = EnemyType.Medium)
    {
        EnemyConfig config = GetConfig(type);
        Enemy instance = CreateGameObjectInstance(config.prefab);
        instance.OriginFactory = this;
        instance.Initialize(
            config.scale.RandomValueInRange, 
            config.speed.RandomValueInRange, 
            config.pathOffSet.RandomValueInRange,
            config.health.RandomValueInRange);
        return instance;
    }

    //回收
    public void Reclaim(Enemy enemy)
    {
        Debug.Assert(enemy.OriginFactory == this, "Wrong factory reclaimed!");
        Destroy(enemy.gameObject);
    }


}